package org.aeon.dao

import org.aeon.entity.User

/**
 * @author:     MissingAeon
 * @date:       2024/1/18 17:00
 * @description: User持久化类
 */
class UserDao : BasicDao<String, User> {
    private companion object {
        val users = mutableMapOf<String, User>()

        init {
            // 这里可以对数据初始化
            users += "root" to User("root", "123456")
            users += "zhangsan" to User("zhangsan", "123456")
            users += "wangwu" to User("wangwu", "123456")
            users += "zhaoliu" to User("zhaoliu", "123456")
            // users += "1" to User("1", "1")
        }
    }

    override fun insert(t: User): Boolean {
        if (!users.containsKey(t.username)) {
            users += t.username to t
            return true
        }
        return false
    }

    override fun modify(t: User): Boolean {
        if (users.containsKey(t.username)) {
            users += t.username to t
            return true
        }
        return false
    }

    override fun remove(t: User): Boolean {
        if (users.containsKey(t.username)) {
            users.remove(t.username)
            return true
        }
        return false
    }

    override fun query(func: (User) -> Boolean): List<User> {
        return users.asSequence()
            .filter { func(it.value) }
            .map { it.value }
            .toList()
    }

    override fun queryById(k: String): User? = users[k]

    override fun exist(k: String): Boolean = users.containsKey(k)

    override fun count(): Int = users.size

}
